<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="制作状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择制作状态" clearable>
          <el-option
            v-for="dict in dict.type.amazon_us_upload_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="运营" prop="optName">
        <el-select v-model="queryParams.optName" placeholder="请选择运营" clearable>
          <el-option
            v-for="dict in dict.type.opt_name_map"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="店铺" prop="shop">
        <el-select v-model="queryParams.shop" placeholder="请选择店铺" clearable>
          <el-option
            v-for="dict in dict.type.amazon_us_shop_list"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-select v-model="queryParams.brand" placeholder="请选择品牌" clearable>
          <el-option
            v-for="dict in dict.type.brand_list"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="货源平台" prop="basicPlatform">
        <el-select v-model="queryParams.basicPlatform" placeholder="请选择货源平台" clearable>
          <el-option
            v-for="dict in dict.type.basic_platform"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="大类目" prop="className">
        <el-select v-model="queryParams.className" placeholder="请选择大类目" clearable>
          <el-option
            v-for="dict in dict.type.amazon_us_class_name"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="小类目" prop="productType">
        <el-input
          v-model="queryParams.productType"
          placeholder="请输入小类目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label=" 修改者" prop="updateBy">
        <el-select v-model="queryParams.updateBy" placeholder="请选择 修改者" clearable>
          <el-option
            v-for="dict in dict.type.update_by_name_list"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="排序方式">
        <el-select v-model="sortForm.sort" placeholder="请选择排序方式" @change="handleSortChange">
          <el-option label="按ID升序" value="id,asc"></el-option>
          <el-option label="按ID降序" value="id,desc"></el-option>
          <el-option label="按修改时间升序" value="updateTime,asc"></el-option>
          <el-option label="按修改时间降序" value="updateTime,desc"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['amazon_us:upload:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['amazon_us:upload:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['amazon_us:upload:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['amazon_us:upload:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="uploadList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="制作状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.amazon_us_upload_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="运营" width="50px" align="center" prop="optName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.opt_name_map" :value="scope.row.optName"/>
        </template>
      </el-table-column>
      <el-table-column label="店铺" align="center" prop="shop">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.amazon_us_shop_list" :value="scope.row.shop"/>
        </template>
      </el-table-column>
      <el-table-column label="品牌" align="center" prop="brand">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.brand_list" :value="scope.row.brand"/>
        </template>
      </el-table-column>
      <el-table-column label="品牌缩写" width="80px" align="center" prop="brandZip">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.brand_zip_list" :value="scope.row.brandZip"/>
        </template>
      </el-table-column>
      <el-table-column label="货源平台" width="80px" align="center" prop="basicPlatform">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.basic_platform" :value="scope.row.basicPlatform"/>
        </template>
      </el-table-column>
      <el-table-column width="80px" label="大类目" align="center" prop="className">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.amazon_us_class_name" :value="scope.row.className"/>
        </template>
      </el-table-column>
      <el-table-column label="小类目" width="80px" align="center" prop="productType"/>
      <el-table-column label="上传数量" width="100px" align="center" prop="uploadLimit"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" width="550px"   align="center" prop="remark"/>
      <el-table-column label=" 修改者" align="center" prop="updateBy">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.update_by_name_list" :value="scope.row.updateBy"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['amazon_us:upload:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['amazon_us:upload:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <BackToTop/>
    <BackToBottom/>
    <!-- 添加或修改上传记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="制作状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.amazon_us_upload_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="店铺" prop="shop">
          <el-select v-model="form.shop" placeholder="请选择店铺" @change="shop_select">
            <el-option
              v-for="dict in dict.type.amazon_us_shop_list"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="运营" prop="optName">
          <el-select v-model="form.optName" placeholder="请选择运营">
            <el-option
              v-for="dict in session_dict.opt_name_map"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-select v-model="form.brand" placeholder="请选择品牌">
            <el-option
              v-for="dict in session_dict.brand_list"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品牌缩写" prop="brandZip">
          <el-select v-model="form.brandZip" placeholder="请选择品牌缩写">
            <el-option
              v-for="dict in session_dict.brand_zip_list"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="货源平台" prop="basicPlatform">-->
<!--          <el-select v-model="form.basicPlatform" placeholder="请选择货源平台">-->
<!--            <el-option-->
<!--              v-for="dict in dict.type.basic_platform"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--            ></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="不上架大类" prop="notClassNameList">
          <el-select v-model="form.notClassNameList" placeholder="请选择不上的大类" multiple>
            <el-option
              v-for="dict in dict.type.amazon_us_class_name"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="小类" prop="productType">-->
<!--          <el-input v-model="form.productType" placeholder="请输入小类"/>-->
<!--        </el-form-item>-->
        <el-form-item label="上传数量" prop="uploadLimit">
          <el-input v-model="form.uploadLimit" placeholder="请输入上传数量"/>
        </el-form-item>
<!--        <el-form-item label="价格" prop="price">-->
<!--          <el-input type="number" v-model.number="form.price" placeholder="请输入至少需要大于该价格(不填默认>=30)"-->
<!--                    :min="0" :max="10000"-->
<!--          ></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="图片数量" prop="limitImg">-->
<!--          <el-input type="number" v-model.number="form.limitImg" placeholder="请输入至少需要图片数量(不填默认3张)"-->
<!--                    :min="1" :max="10"-->
<!--          ></el-input>-->
<!--        </el-form-item>-->
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addUpload, delUpload, getUpload, listUpload, updateUpload } from '@/api/amazon_us/upload'
import globalMixins from '@/utils/globalMixins'

export default {
  name: 'Upload',
  dicts: ['update_by_name_list', 'opt_name_map', 'amazon_us_shop_list', 'brand_zip_list', 'brand_list', 'amazon_us_upload_status', 'amazon_us_class_name', 'basic_platform'],
  data() {
    return {
      pickerOptions: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 上传记录表格数据
      uploadList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 限制数量时间范围
      daterangeCreateTime: [],
      // 查询参数
      sortForm: {
        sort: 'id,desc' // 默认值
      },
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        status: null,
        optName: null,
        shop: null,
        brand: null,
        basicPlatform: null,
        className: null,
        productType: null,
        createTime: null,
        updateBy: null
      },
      // 表单参数
      form: {
        notClassNameList:["Clothes & Decoration", "Jewelry","Automotive","Media products - books DVD Music software video"]
      },
      // 表单校验
      rules: {
        optName: [
          { required: true, message: '运营不能为空', trigger: 'change' }
        ],
        shop: [
          { required: true, message: '店铺不能为空', trigger: 'change' }
        ],
        brand: [
          { required: true, message: '品牌不能为空', trigger: 'change' }
        ],
        brandZip: [
          { required: true, message: '品牌缩写不能为空', trigger: 'change' }
        ],
      },
      config: {},
      session_dict: {
        amazon_us_shop_list: [],
        opt_name_map: [],
        brand_list: [],
        brand_zip_list: []
      }
    }
  },
  created() {
    this.pickerOptions = globalMixins.data().pickerOptions
    this.$set(this.queryParams, 'orderByColumn', 'id')
    this.$set(this.queryParams, 'isAsc', 'desc')
    this.getConfigKey('amazon_us_shop_map').then(response => {
      this.config = JSON.parse(response.msg)
      this.session_dict.shop_list = this.dict.type.amazon_us_shop_list
      this.session_dict.opt_name_map = this.dict.type.opt_name_map
      this.session_dict.brand_list = this.dict.type.brand_list
      this.session_dict.brand_zip_list = this.dict.type.brand_zip_list
    })
    this.getList()
  },
  methods: {
    handleSortChange(value) {
      const [field, order] = value.split(',')
      // 更新查询参数
      this.$set(this.queryParams, 'orderByColumn', field)
      this.$set(this.queryParams, 'isAsc', order)
      // 根据新的查询参数重新获取列表数据
      this.getList()
    },
    /** 店铺发生变更时候 */
    shop_select(shopValue) {
      this.updateOptionsBasedOnShopSelection(shopValue)
    },
    updateOptionsBasedOnShopSelection(shopValue) {
      const selectedShopConfig = this.config[shopValue] || {}

      // 更新品牌选项
      this.session_dict.brand_list = this.filterOptionsByConfig(this.dict.type.brand_list, 'brand', selectedShopConfig)

      // 更新品牌缩写选项
      this.session_dict.brand_zip_list = this.filterOptionsByConfig(this.dict.type.brand_zip_list, 'brand_zip', selectedShopConfig)

      // 更新操作人选项
      this.session_dict.opt_name_map = this.filterOptionsByConfig(this.dict.type.opt_name_map, 'opt_name', selectedShopConfig)
    },
    filterOptionsByConfig(allOptions, optionType, shopConfig) {
      if (!shopConfig[optionType]) return allOptions
      let showSet = new Set(shopConfig[optionType])
      let filteredList = allOptions.filter(item => showSet.has(item.label))
      return filteredList
    },

    /** 查询上传记录列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params['beginCreateTime'] = this.daterangeCreateTime[0]
        this.queryParams.params['endCreateTime'] = this.daterangeCreateTime[1]
      }
      listUpload(this.queryParams).then(response => {
        this.uploadList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        status: null,
        optName: null,
        shop: null,
        brand: null,
        brandZip: null,
        basicPlatform: null,
        className: null,
        notClassNameList:["Clothes & Decoration", "Jewelry","Automotive","Media products - books DVD Music software video"],
        productType: null,
        uploadLimit: null,
        price: null,
        limitImg: null,
        createTime: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加上传记录'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getUpload(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改上传记录'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUpload(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addUpload(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除上传记录编号为"' + ids + '"的数据项？').then(function() {
        return delUpload(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('amazon_us/upload/export', {
        ...this.queryParams
      }, `upload_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
